package com.youmu.sichuansfc.service.impl;

import com.youmu.sichuansfc.dao.FundUserDao;
import com.youmu.sichuansfc.dao.FundUserLoginHistoryDao;
import com.youmu.sichuansfc.domain.YoumuFundUser;
import com.youmu.sichuansfc.domain.YoumuFundUserLoginHistory;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.service.FundUserService;
import com.youmu.sichuansfc.util.Const;
import com.youmu.sichuansfc.util.MD5;
import com.youmu.sichuansfc.util.NetworkUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class FundUserServiceImpl implements FundUserService {

    @Autowired
    FundUserDao fundUserDao;
    @Autowired
    FundUserLoginHistoryDao fundUserLoginHistoryDao;

    @Override
    public YoumuFundUser findByToken(String token) {
        return fundUserDao.findByToken(token);
    }

    @Override
    public YoumuFundUser login(String phone, String password, HttpServletRequest httpServletRequest) {

        YoumuFundUser youmuFundUser = fundUserDao.findByPhone(phone);
        if(youmuFundUser != null){
            String pas = youmuFundUser.getPassword();
            // TODO登录成功
            if(pas.equals(password) ||  pas.equals(MD5.getMD5Code(password, Const.FUND_USER_MD5_KEY)) ){
                String token = youmuFundUser.getToken();
                if (!StringUtils.isNotEmpty(token)){
                    String newToken = UUID.randomUUID().toString().replaceAll("-", "");
                    youmuFundUser.setToken(newToken);
                    youmuFundUser = fundUserDao.update(youmuFundUser);
                }
                //登录IP
                String loginIp = "";
                try {
                    loginIp = NetworkUtil.getIpAddress(httpServletRequest);
                } catch (Exception e) {
                    e.printStackTrace();
               }
                // 添加登录记录
                if(youmuFundUser.getIsDelete()) {
                    fundUserLoginHistoryDao.insert(new Date(), loginIp, youmuFundUser.getId());
                }
                return youmuFundUser;
            }
        }
     return null;
    }

    @Override
    public YoumuFundUser update(YoumuFundUser youmuFundUser) {
        return fundUserDao.update(youmuFundUser);
    }

    @Override
    public PageT getFundUsers(Integer fundUserStatus, String fundManagerName, int num, int size) {
        return fundUserDao.findFundUsers( fundUserStatus,  fundManagerName,  num,  size);
    }

    @Override
    public YoumuFundUser findById(Long id) {
        return fundUserDao.findById(id);
    }

    @Override
    public List<YoumuFundUserLoginHistory> findLoginHistory(Long fundUserId, int num, int size) {
        return fundUserLoginHistoryDao.findByFundUserId(fundUserId, num, size);
    }


}
